
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>App列表</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
  <style>

    /*    .layui-table-cell {*/
    /*      font-size: 14px;*/
    /*      padding: 0 5px;*/
    /*      height: auto;*/
    /*      overflow: visible;*/
    /*      text-overflow: inherit;*/
    /*      white-space: normal;*/
    /*      word-break: break-all;*/
    /*    }*/

  </style>
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <div class="layui-inline">
          <input type="text" name="title" placeholder="标题" autocomplete="off" class="layui-input">
        </div>

        <div class="layui-inline">
          <select name="quick_id" lay-search>
            <option value="">选择App</option>
            {foreach $quickApps as $vo}
            <option value="{$vo.id}">{$vo.name}</option>
            {/foreach}
          </select>
        </div>

        <div class="layui-inline">
          <input type="text" name="channel_id" placeholder="投放号id" autocomplete="off" class="layui-input">
        </div>

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>

        {if $role->alias == 'channel' || $role->alias == 'agent'}
        <div class="layui-inline" style="float: right;">
          <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
        </div>
        {/if}


      </div>
    </div>
    <div class="layui-card-body">

      <table id="LAY-user-back-role" lay-filter="LAY-user-back-role"></table>


      <script type="text/html" id="typeTpl">
        {{#  if(d.msg_type == 'continue'){ }}
        <span class="layui-badge layui-bg-gray">继续阅读</span>
        {{#  } }}
        {{#  if(d.msg_type == 'sales'){ }}
        <span class="layui-badge layui-bg-cyan">促销活动</span>
        {{#  } }}
        {{#  if(d.msg_type == 'novel'){ }}
        <span class="layui-badge layui-bg-blue">小说推荐</span>
        {{#  } }}
        {{#  if(d.msg_type == 'sign'){ }}
        <span class="layui-badge layui-bg-green">签到提醒</span>
        {{#  } }}
      </script>


      <script type="text/html" id="table-useradmin-admin">
        <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>

      <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
          <button class="layui-btn layui-btn-primary layui-border-red layui-btn-sm" lay-event="getCheckData">批量删除</button>
        </div>
      </script>

    </div>
  </div>
</div>


<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
  layui.config({
    base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','table'], function(){
    var $ = layui.$
            ,form = layui.form
            ,table = layui.table
            ,admin = layui.admin;

    //监听搜索
    form.on('submit(LAY-user-back-search)', function(data){
      var field = data.field;

      //执行重载
      table.reload('LAY-user-back-role', {
        where: field
        ,page: {
          curr: 1  // 将页码重置为第一页
        }
      });
    });

    table.render({
      elem: '#LAY-user-back-role'
      , url: '/push/list?push_type=1' //模拟接口
      , response: {
        statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
      }
      , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
      , defaultToolbar: false
      , page: true //开启分页
      , cols: [[
        {type: 'checkbox'}
        , {field: 'id', width: 80, title: 'ID'}
        , {field: 'title', title: '标题'}
        , {field: 'describe', title: '描述'}
        , {
          field: 'quick_app', title: 'App', templet: function (d) {
            if (d.quick_app) {
              //得到当前行数据，并拼接成自定义模板
              return d.quick_app.name
            } else {
              return '';
            }
          }
        }
        , {
          field: 'channel', title: '投放号', templet: function (d) {
            if (d.channel) {
              //得到当前行数据，并拼接成自定义模板
              return d.channel.account
            } else {
              return '';
            }
          }
        }
        , {field: 'msg_type', title: '类型', templet: '#typeTpl', align: 'center'}
        , {field: 'push_delay_minutes', title: '加桌后*分推送', align: 'center',templet:function (d) {
            return d.push_delay_minutes + "分";
          }}
        , {field: 'create_time', title: '创建时间'}
        , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
      ]]
    });


    //监听行工具事件
    table.on('tool(LAY-user-back-role)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('确定删除？',{title: false}, function(index){
          admin.req({
            url: '/push/delPush'
            , type: "POST"
            , data: {'ids':[data.id]}
            , done: function (res) {
              layer.msg('删除成功', {icon: 1, time: 1000}, function () {
                obj.del();
                layer.close(index);
              });
            }, not_finish: function (res) {
              layer.msg(res.msg);
            }
          });
        });
      }else if(obj.event === 'edit'){
        var tr = $(obj.tr);
        layer.open({
          type: 2
          , title: '修改App'
          , content: '/quick_app/quickAppForm.html?id=' + obj.data.id
          , area: ['500px', '480px']
          , btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              var field = data.field; //获取提交的字段
              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              admin.req({
                url: '/quick_app/setQuickApp'
                , type: "POST"
                , data: field
                , done: function (res) {
                  layer.msg('修改成功', {icon: 1, time: 1000}, function () {
                    table.reload('LAY-user-back-role');
                    layer.close(index); //关闭弹层
                  });
                },not_finish:function (res) {
                  layer.msg(res.msg);
                }
              });
            });
            submit.trigger('click');
          }
        });
      }
    });



    //头工具栏事件
    table.on('toolbar(LAY-user-back-role)', function (obj) {
      var checkStatus = table.checkStatus(obj.config.id);
      switch (obj.event) {
        case 'getCheckData':
          var data = checkStatus.data;
          if (data.length === 0) {
            layer.msg('请选择需要删除的推送');
            return false;
          }
          var ids = [];
          for (var key in data) {
            ids.push(data[key]['id']);
          }
          layer.confirm('确定删除....？',{title: false}, function(index){
            admin.req({
              url: '/push/delPush'
              , type: "POST"
              , data: {'ids':ids}
              , done: function (res) {
                layer.msg('删除成功', {icon: 1, time: 1000}, function () {
                  layer.closeAll();
                  table.reload('LAY-user-back-role');
                });
              }, not_finish: function (res) {
                layer.msg(res.msg);
              }
            });
          });
          break;
      }
    });
  });
</script>


</body>
</html>

